package com.fsserver.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * MyBatis-Plus配置类
 * MyBatis-Plus Configuration
 *
 * @author fsserver
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     * Pagination Interceptor
     *
     * @return MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件
        // Add pagination interceptor
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 自动填充处理器
     * Auto Fill Handler
     *
     * @return MetaObjectHandler
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                // 创建时间自动填充
                // Auto fill create time
                this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
                // 更新时间自动填充
                // Auto fill update time
                this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
                // 是否删除自动填充为0（未删除）
                // Auto fill is_deleted as 0 (not deleted)
                this.strictInsertFill(metaObject, "isDeleted", Integer.class, 0);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                // 更新时间自动填充
                // Auto fill update time
                this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
            }
        };
    }
} 